package com.ruoyi.project.system.keystore.controller;

import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.auth.service.AuthService;
import com.ruoyi.project.system.keystore.domain.Keystore;
import com.ruoyi.project.system.keystore.service.KeystoreService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 认证信息
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/keystore")
public class KeystoreController extends BaseController {
    private String prefix = "system/keystore";

    @Autowired
    private KeystoreService keystoreService;

    @Autowired
    private AuthService authService;

    @RequiresPermissions("system:keystore:view")
    @GetMapping()
    public String keystore() {
        return prefix + "/keystore";
    }

    @RequiresPermissions("system:keystore:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Keystore keystore) {
        startPage();
        List<Keystore> list = keystoreService.selectKeystoreList(keystore);
        return getDataTable(list);
    }


    /**
     * 新增keystore
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存角色
     */
    @RequiresPermissions("system:keystore:add")
    @Log(title = "keystore管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Keystore keystore) {
        if (!keystoreService.checkKeystoreNameUnique(keystore)) {
            return error("新增keystore'" + keystore.getKeystoreName() + "'失败，角色名称已存在");
        }

        int rows = keystoreService.insertKeystore(keystore);
        if (rows == -1) {
            return error("新增keystore'" + keystore.getKeystoreName() + "'失败，生成文件失败");
        }
        return toAjax(rows);
    }

    /**
     * 修改认证
     */
    @RequiresPermissions("system:keystore:edit")
    @GetMapping("/edit/{keystoreId}")
    public String edit(@PathVariable("keystoreId") Long keystoreId, ModelMap mmap) {
        Keystore keystore = keystoreService.selectKeystoreById(keystoreId);
        mmap.put("keystore", keystore);

        return prefix + "/edit";
    }

    /**
     * 修改保存认证
     */
    @RequiresPermissions("system:keystore:edit")
    @Log(title = "keystore管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Keystore keystore) {
        return toAjax(keystoreService.updateKeystore(keystore));
    }

    @RequiresPermissions("system:keystore:remove")
    @Log(title = "Keystore管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        Long[] keystoreIds = Convert.toLongArray(ids);
        if (keystoreIds.length < 1) {
            return AjaxResult.warn("请选择需要删除的数据");
        }

        List<Keystore> keystores = keystoreService.selectByIds(keystoreIds);
        if (keystores.stream().map(Keystore::getInitial).anyMatch(initial -> Objects.equals(initial, 0))) {
            return AjaxResult.warn("存在默认Keystore，不允许删除");
        }

        if (authService.existByKeystoreIds(keystoreIds)) {
            return AjaxResult.warn("Keystore已被使用，不允许删除");
        }

        return toAjax(keystoreService.deleteKeystoreByIds(keystoreIds));
    }


    /**
     * 校验keystore名称
     */
    @PostMapping("/checkKeystoreNameUnique")
    @ResponseBody
    public boolean checkKeystoreNameUnique(Keystore keystore) {
        return keystoreService.checkKeystoreNameUnique(keystore);
    }
}